<template>
    <div class="pay">
        <div class="vh-c hyrz">
            会员认证
        </div>
        <div class="vh-c wptq">
            【王牌特权】推广扫码即可领取现金，商家限时打款！
        </div>
        <div class="vh-c amount">
            <div>
                {{userInfo.amount?userInfo.amount:300}}
            </div>
            <div>
                元
            </div>
        </div>
        <div class="vh-c img">
            <image src="../../static/imgs/pay.png" mode="aspectFit"></image>

        </div>
        <u-button type="primary" text="去支付" :disabled="disabledbtn" @click="submit()" style="margin-top:20rpx !important;">
        </u-button>
    </div>
</template>
<script>
export default {
    data() {
        return {
            userInfo: {},
            disabledbtn: false,
            opget: {}

        }
    },
    onShow() {
        this.userInfo = this.$store.state.userInfo ? this.$store.state.userInfo : ''
        wx.login({
            success: (res) => {
                if (res.code) {
                    this.$http.get('getOpenid/' + res.code).then(res1 => {
                        console.log(res1)
                        this.opget = res1
                    }, err => {
                        loginStart = true;
                    });
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    },
    methods: {
        async submit() {
            let val=await this.gotoPay()
            await this.requestPayment(val)
            await this.certification()
            await this.login()
        },
        gotoPay() {
            let that = this
            this.disabledbtn = true
            return new Promise((resolve, reject) => {
                this.$http.post('/wx/pay/wx_unified_order', {
                    openid: this.opget.data.openid
                }).then(val => {
                    resolve(val)
                }).catch(err => {

                    that.disabledbtn = false
                })
            })

        },
        requestPayment(val) {
            let that = this
            return new Promise((resolve, reject) => {
                uni.requestPayment({
                    provider: 'wxpay',    //支付类型-固定值
                    timeStamp: val.data.data.timeStamp, // 时间戳（单位：秒）
                    nonceStr: val.data.data.nonceStr, // 随机字符串
                    package: val.data.data.package, // 固定值
                    signType: val.data.data.signType, //固定值
                    paySign: val.data.data.paySign, //签名
                    success: function (res) {
                        resolve()
                    },
                    fail: function (err) {
                        that.disabledbtn = false
                        uni.showToast({
                            title: '支付失败',
                            icon: "error",
                            duration: 2000
                        });
                        console.log('fail:' + JSON.stringify(err));
                        console.log("支付失败");
                    }
                });
            })
        },
        //认证
        certification() {
            let that = this
            return new Promise((resolve, reject) => {
                that.$http.post('/app/user_info/certification', {type:'1'}).then(val => {
                    if (val.code == 200) {
                        resolve()
                    }
                    else {
                        uni.showToast({
                            title: val.message,
                            icon: "none",
                            duration: 2000
                        });
                        that.disabledbtn = false
                    }
                }).catch(errors => {
                    that.disabledbtn = false
                })
            })
        },
        //登录
        login() {
            let that = this

            return new Promise((resolve, reject) => {
                that.$http.post('login', {
                    mobile: uni.getStorageSync('weChatMobile')
                }).then(val => {
                    that.$store.commit('setUserInfo', val.data);
                    uni.navigateBack();
                    that.disabledbtn = false
                    uni.showToast({
                        title: '支付成功',
                        icon: "success",
                        duration: 2000
                    });
                }).catch(err => {
                    this.disabledbtn = false
                })
            })
        }
    }
}
</script>
<style lang="scss">
.pay {
    .hyrz {
        color: #004ee8;
        padding: 5vh 0;
        font-size: 28rpx;
    }
    .wptq {
        padding: 1vh 0;
        color: #004ee8;
        font-size: 28rpx;
    }
    .amount {
        padding: 5vh 0;
        color: #000;
        align-items: flex-end;
        line-height: 1;
        div:nth-child(1) {
            font-size: 80rpx;
        }
        div:nth-child(2) {
            font-size: 30rpx;
            position: relative;
            top: -10rpx;
        }
    }
    .img {
        height: 20vw;
        margin-bottom: 3vh;
        image {
            width: 90vw;
            height: 20vw;
        }
    }
    button {
        margin: 0 auto;
        width: 90vw !important;
        background-color: #004ee8 !important;
    }
}
</style>